package com.twu.biblioteca;

import com.twu.io.IOHandler;
import com.twu.user.User;
import com.twu.user.UserDatabase;

import java.util.ArrayList;
import java.util.concurrent.Exchanger;

/**
 * Created by thoughtworks on 27/7/14.
 */
public class Library {

    private static final String BOOK = "BOOK";
    private ArrayList<Book> books = new ArrayList();
    private ArrayList<Movie> movies = new ArrayList();
    private int loggedInUser;

    public Library() {
    }


    private boolean returnItem(String name, ArrayList items) throws Exception {
        Item item;
        if((item = find(name, items)) != null) {
            if (!item.isAvailable()) {
                return item.checkIn();
            }
            throw new Exception("Movie is already available!");
        }
        throw new Exception("Invalid item name!");
    }
    private boolean checkOut(String name, ArrayList Type) throws Exception {
        Item item;
        if((item = find(name,Type)) != null)
        {
            if(item.isAvailable())
            {
                item.assignedTo(loggedInUser);
                return item.checkOut();
            }
            throw new Exception("Not available!");
        }
        throw new Exception("Invalid name!");
    }
    private Item find(String name, ArrayList<Item> list) {
        for (Item item : list)
        {
            if(item.getName().equalsIgnoreCase(name))
            {
                return item;
            }

        }
        return null;
    }

//-------------------------------------------------

    public void addNewBook(String name,  String author,String publisher,String yop) {
        Book book = new Book(name,author,publisher,yop);
        books.add(book);
    }

    public boolean checkOutBook(String bookName) throws Exception{
        return checkOut(bookName,books);
        //return false;
    }
    public boolean returnBook(String name) throws Exception{
        return returnItem(name,books);
    }


    public ArrayList getCheckedOutBookList() {
        ArrayList<Book> checkedOutBooks = new ArrayList<Book>();
        for (Book b : books) {
            if(!b.isAvailable())
                checkedOutBooks.add(b);
        }
        return checkedOutBooks;
    }
    public ArrayList getAvailableBookList() {
        ArrayList<Book> availableBooks = new ArrayList<Book>();
        for (Book b : books) {
            if(b.isAvailable())
                availableBooks.add(b);
        }
        return availableBooks;
    }
//-----------------------------------------------------------------

    public ArrayList<Movie> getAvailableMovieList() {
        ArrayList<Movie> availableMovies = new ArrayList();
        for (Movie movie : movies) {
            if(movie.isAvailable())
                availableMovies.add(movie);
        }
        return availableMovies;
    }

    public boolean checkOutMovie(String name) throws Exception {
        return checkOut(name,movies);
        }



    public boolean returnMovie(String name) throws Exception {
        return returnItem(name,movies);
    }



    public void addNewMovie(String name, String producer, String director, String yop) {
        Movie movie = new Movie(name,producer,director,yop);
        movies.add(movie);
    }


    public void setLoggedInUser(int loggedInUser) {
        this.loggedInUser = loggedInUser;
    }

    public void displayCheckoutUserList(UserDatabase userDB) {
        for(Item item : books)
        {
            IOHandler io = new IOHandler();
            io.displayString("book " + item.getName() + " checked out by " + userDB.getUserById(item.getAssignedTo()));
        }
        for(Item item : movies)
        {
            IOHandler io = new IOHandler();
            io.displayString("movie " + item.getName() + " checked out by " + userDB.getUserById(item.getAssignedTo()));
        }
    }
}
